const assert = require("assert");
const Param = require("../src/param.js");

describe("param.js", () => {
  describe("checkParams()", () => {
    it("not empty", async () => {
      try {
        await Param.checkParams([["m", "add", "isNotEmpty", "请正确输入方法"]]);
        assert(true);
      } catch (e) {
        console.log(e);
        assert(false);
      }
    });
    it("undefinded", async () => {
      let value;
      try {
        await Param.checkParams([["m", value, "require", "请正确输入方法"]]);
        assert(false);
      } catch (e) {
        console.log(e);
        assert(true);
      }
    });
    it("int", async () => {
      try {
        await Param.checkParams([["m", "12", "isInt", "请正确输入数字"]]);
        assert(true);
      } catch (e) {
        console.log(e);
        assert(false);
      }
    });
    it("notInt but not require", async () => {
      try {
        await Param.checkParams([["m", "", "isInt", "请正确输入数字"]]);
        assert(true);
      } catch (e) {
        console.log(e);
        assert(false);
      }
    });
    it("get params", async () => {
      let params = await Param.checkParams([
        ["m", "add", "isNotEmpty", "请正确输入方法"],
      ]);
      assert.deepStrictEqual(params, { m: "add" });
    });
    it("require", async () => {
      let value;
      try {
        await Param.checkParams([["m", value, "require", "请正确输入方法"]]);
        assert(false);
      } catch (e) {
        console.log(e);
        assert(true);
      }
    });
    it("empty string", async () => {
      let value = "";
      try {
        await Param.checkParams([["m", value, "require", "请正确输入方法"]]);
        assert(false);
      } catch (e) {
        console.log(e);
        assert(true);
      }
    });
    it("2 checkers", async () => {
      let value = "0";
      try {
        await Param.checkParams([
          ["m", value, "require isInt", "请正确输入方法"],
        ]);
        assert(true);
      } catch (e) {
        console.log(e);
        assert(false);
      }
    });
    it("default value", async () => {
      let value = "";
      try {
        let params = await Param.checkParams([
          ["m", value, "isInt", "请正确输入方法", 2],
        ]);
        assert(params.m == 2);
      } catch (e) {
        console.log(e);
        assert(false);
      }
    });
    it("check or", async () => {
      let value = [undefined, undefined];
      try {
        await Param.checkParams([["", value, "require", "请正确输入其中一个"]]);
        assert(false);
      } catch (e) {
        console.log(e);
        assert(true);
      }
    });
  });
  describe("isBlank()", () => {
    it("not empty", async () => {
      try {
        assert(!Param.isBlank(0));
      } catch (e) {
        console.log(e);
        assert(false);
      }
    });
    it("undefined", async () => {
      try {
        let value;
        assert(Param.isBlank(value));
      } catch (e) {
        console.log(e);
        assert(false);
      }
    });
    it("empty", async () => {
      try {
        let value = "";
        assert(Param.isBlank(value));
      } catch (e) {
        console.log(e);
        assert(false);
      }
    });
    it("null", async () => {
      try {
        let value = null;
        assert(Param.isBlank(value));
      } catch (e) {
        console.log(e);
        assert(false);
      }
    });
  });
});
